package com.google.code.roledroid;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.app.ListActivity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CharacterListActivity extends ListActivity {
	List<String> items= new ArrayList<String>();
	String[] ids = new String[0];
	CharacterDBHelper Db = null;
	private ArrayAdapter<String> adapter;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 	Initializing the database
		Db = new CharacterDBHelper(this);

		items = Db.getColumn("name");        
		ids = Db.getColumn("_id").toArray(ids);

		// Closing Database
		Db.close();

		setContentView(R.layout.activity_character_list);

		adapter = new ArrayAdapter<String>(this,
				R.layout.character_list_row, R.id.label,
				items);
		setListAdapter(adapter);

		registerForContextMenu(getListView());
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_character_list_context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info;
		try {
			info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		} catch (ClassCastException e) {
			Log.e("", "bad menuInfo", e);
			return false;
		}
		long id = getListAdapter().getItemId(info.position);

		// TODO: ask confirmation and error treatment

		Log.d("", "Deleting id = " + id);       

		// Connecting to DataBase
		Db = new CharacterDBHelper(this);

		// Updating lists For If the Flies :P
		items = Db.getColumn("name");        
		ids = Db.getColumn("_id").toArray(ids);
				
		// Deleting Database Row
		Long rowId = Long.valueOf(ids[info.position]);
		Db.deleteRow(rowId);

		// Updating lists
		items = Db.getColumn("name");        
		ids = Db.getColumn("_id").toArray(ids);
		
		// Updating display.
		adapter.remove(adapter.getItem(info.position));		
		adapter.notifyDataSetChanged();

		// Closing Database
		Db.close();		

		return true;
	}

	public void onListItemClick(ListView parent, View v,
			int position, long id) {    	
		Toast.makeText(this, "You want to edit the character "+items.get(position), Toast.LENGTH_SHORT).show();

		Intent i = new Intent(CharacterListActivity.this, CharacterEditActivity.class);
		i.putExtra("rowId", Long.valueOf(ids[position]));
		i.putExtra("listItemId", position);

		startActivityForResult(i, 1);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Returning form edition?  Must refresh!

		if (requestCode == 1) {

			if(resultCode == RESULT_OK){

				String name = data.getStringExtra("resultName");
				int position = data.getIntExtra("listItemId",-1);

				if (position != -1) {
					adapter.remove(adapter.getItem(position));
					adapter.insert(name, position);
					adapter.notifyDataSetChanged();
				}
			}
		}
	}

}
